<?php
/**
 * WB_S3Response_BucketList class
 * 
 * @author Jean-Lou Dupont
 * @version @@package-version@@
 * @id $Id: S3Response_BucketList.php 143 2008-03-14 18:53:35Z jldupont $
 */

 //{{include
 require_once 'web-bloks/AmazonS3/responses/S3Response.php';
 require_once 'web-bloks/AmazonS3/info/S3BucketInfo.php'; 
 require_once 'web-bloks/ObjFactory/ObjFactory.php';
 //include}}
 
 class WB_S3Response_BucketList
	extends WB_S3Response 
 {
 	/**
 	 * Constructor
 	 */
	public function __construct( &$rawResponse )
	{
		parent::__construct( $rawResponse );
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%		
	// INTERNAL methods
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%				
	protected function init()
	{
		$buckets = $this->extractBuckets();
		if ( is_null ( $buckets ))
			return;
		
		foreach( $buckets as $index =>$raw )
		{
			$name = null;
			$creation = null;
			$this->extractNameAndCreation( $raw, $name, $creation );
			
			if ( !empty( $name ))
			{
				$info = WB_ObjFactory::factory( 'WB_S3BucketInfo' );
				$info->initFromRaw( $raw );
				$this->liste[ $name ] = $info ;
			}
		}
		return $this;
	}
	protected function extractBuckets()
	{
		if ( empty( $this->raw ) )
			return null;
			
		$r = preg_match_all("@<"."Bucket>(.*?)<"."/Bucket>@", $this->raw, $matches);
		if (($r === false) || ($r === 0))
			return null;
			
		return $matches[1];			
	}
	protected function extractNameAndCreation( &$raw, &$name, &$creation )	
	{
		if ( empty( $raw ) )
			return;
			
		preg_match("@<"."Name>(.*?)<"."/Name>@", $raw, $nameMatch );
		preg_match("@<"."CreationDate>(.*?)<"."/CreationDate>@", $raw, $creationMatch );
		
		$name = $nameMatch[1];
		$creation = $creationMatch[1];
	}
	
 } // end class declaration
 
 /* SAMPLE RESPONSE (from s3-dg-20060301.pdf)
    -----------------------------------------
	<?xml version="1.0" encoding="UTF-8"?>
	<ListAllMyBucketsResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
		<Owner>
			<ID>bcaf1ffd86f41caff1a493dc2ad8c2c281e37522a640e161ca5fb16fd081034f</ID>
			<DisplayName>webfile</DisplayName>
		</Owner>
	<Buckets>
		<Bucket>
			<Name>quotes;/Name>
			<CreationDate>2006-02-03T16:45:09.000Z</CreationDate>
		</Bucket>
		<Bucket>
			<Name>samples</Name>
			<CreationDate>2006-02-03T16:41:58.000Z</CreationDate>
		</Bucket>
	</Buckets>
	</ListAllMyBucketsResult>
 */